//
//  ButtomCollectionViewCell.m
//  礼物说
//
//  Created by dllo on 16/4/21.
//  Copyright © 2016年 sunzhichao. All rights reserved.
//

/** 此页是cell的铺设*/
#import "ButtomCollectionViewCell.h"

@interface ButtomCollectionViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) NSMutableArray *lunBoZhongZhuanArr;
@property (nonatomic, retain) NSMutableArray *jingXuanZhongZhuanArr;
@property (nonatomic, retain) NSMutableArray *zhuanTiZhongZhuanArr;
@property (nonatomic, retain) NSMutableArray *lunBoModelArr;

@property (nonatomic, assign) NSInteger num;

@end

@implementation ButtomCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createCollection];
    }
    return self;
}
- (void)dealloc {
    [_lunBoModelArr release];
    [_zhuanTiModelArr release];
    [_zhuanTiZhongZhuanArr release];
    [_jingXuanZhongZhuanArr release];
    [_lunBoZhongZhuanArr release];
    [_jingXuanArr release];
    [_lunBoArr release];
    [_collectionView release];
    [super dealloc];
}
/** 轮播*/
- (void)setLunBoArr:(NSMutableArray *)lunBoArr {
    if (_lunBoArr != lunBoArr) {
        [_lunBoArr release];
        _lunBoArr = [lunBoArr retain];
    }
    self.lunBoModelArr = _lunBoArr;
    self.lunBoZhongZhuanArr = [NSMutableArray array];
    for (LunBoModel *model in _lunBoArr) {
        NSString *url = model.image_url;
        [_lunBoZhongZhuanArr addObject:url];
    }
    NSLog(@"%@", _lunBoZhongZhuanArr);
}
/** 精选*/
- (void)setJingXuanArr:(NSMutableArray *)jingXuanArr {
    if (_jingXuanArr != jingXuanArr) {
        [_jingXuanArr release];
        _jingXuanArr = [jingXuanArr retain];
    }
    self.jingXuanZhongZhuanArr = [NSMutableArray arrayWithArray:_jingXuanArr];
    NSLog(@"%@", _jingXuanZhongZhuanArr);
}
/** 专题*/
- (void)setZhuanTiModelArr:(NSMutableArray *)zhuanTiModelArr {
    if (_zhuanTiModelArr != zhuanTiModelArr) {
        [_zhuanTiModelArr release];
        _zhuanTiModelArr = [zhuanTiModelArr retain];
    }
    self.zhuanTiZhongZhuanArr = [NSMutableArray arrayWithArray:_zhuanTiModelArr];
}

#pragma mark - 创建集合
- (void)createCollection {
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 1;
    layout.minimumLineSpacing = 1;
    layout.itemSize = CGSizeMake(kWIDTHCELL, kWIDTHS(300));
    layout.sectionInset = UIEdgeInsetsMake(1, 1, 1, 1);
    
    self.collectionView = [[BaseCollectionView alloc] initWithFrame:self.contentView.frame collectionViewLayout:layout];
    [self.contentView addSubview:_collectionView];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.pagingEnabled = NO;
    _collectionView.bounces = YES;
    [_collectionView release];
    [layout release];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    [_collectionView registerClass:[LunBoCollectionViewCell class] forCellWithReuseIdentifier:@"lunBoCell"];
    
    [_collectionView registerClass:[ZhanTiGunDongCollectionViewCell class] forCellWithReuseIdentifier:@"zhaunTiCell"];
    
    [_collectionView registerClass:[ContentCollectionViewCell class] forCellWithReuseIdentifier:@"contentCell"];
    
    [self createMjbfresh];
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    [super layoutSubviews];
}

/** 上拉刷新，下拉加载*/
- (void)createMjbfresh {
    _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [[NSNotificationCenter defaultCenter] postNotificationName:@"下拉刷新" object:_collectionView];
        NSString *index = [NSString stringWithFormat:@"%ld", (long)_index];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"下拉刷新" object:nil userInfo:@{@"collectionview" : _collectionView , @"index": index}];
    }];
    self.num = 0;
    _collectionView.mj_footer = [MJRefreshBackFooter footerWithRefreshingBlock:^{
        
        _num = _num + 20;
        NSString * str = [NSString stringWithFormat:@"%ld", (long)_num];
        NSString * index = [NSString stringWithFormat:@"%ld", (long)_index];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"上拉加载" object:nil userInfo: @{@"num" : str, @"collectionview" : _collectionView, @"index" : index }];
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _jingXuanZhongZhuanArr.count + 2;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        LunBoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"lunBoCell" forIndexPath:indexPath];
        NSLog(@"%@", _lunBoZhongZhuanArr);
        cell.urlArr = _lunBoZhongZhuanArr;
        cell.modelArr = _lunBoModelArr;
        cell.backgroundColor = [UIColor grayColor];
        return cell;
    }else if (indexPath.row == 1) {
        ZhanTiGunDongCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"zhaunTiCell" forIndexPath:indexPath];
        cell.modelArr = _zhuanTiZhongZhuanArr;
        return cell;
    }else {
        ContentCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"contentCell" forIndexPath:indexPath];
        cell.model = _jingXuanZhongZhuanArr[indexPath.row - 2];
        NSLog(@"%@", _jingXuanZhongZhuanArr);
        return cell;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return CGSizeMake(kWIDTHCELL, kHEIGHTS(150));
    }else if (indexPath.row == 1) {
        return CGSizeMake(kWIDTHCELL, kHEIGHTS(100));
    }else {
        return CGSizeMake(kWIDTHCELL, kHEIGHTS(200));
    }
}

#pragma mark - collection的点击方法[攻略]
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row > 0) {
        GongLvXiangQingViewController *gongLvVc = [[GongLvXiangQingViewController alloc] init];
        ContentModel *model = _jingXuanZhongZhuanArr[indexPath.row - 2];
        gongLvVc.number = model.number;
        UIViewController *viewController = [self viewController];
        [viewController.navigationController pushViewController:gongLvVc animated:YES];
        [gongLvVc release];
    }
}

/**获取父视图的方法 */
- (UIViewController *)viewController {
    for (UIView *next = [self superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}

@end
